package cn.ncist.health_service_provider.service.impl;

import cn.ncist.dto.OrderDto;
import cn.ncist.health_interface.service.OrderService;
import cn.ncist.health_service_provider.dao.MemberMapper;
import cn.ncist.health_service_provider.dao.OrderMapper;
import cn.ncist.health_service_provider.dao.OrderSettingMapper;
import cn.ncist.pojo.Member;
import cn.ncist.pojo.Order;
import cn.ncist.pojo.OrderSetting;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Slf4j
@Service
@Transactional
@DubboService(interfaceClass = OrderService.class, timeout = 50000)
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Integer order(OrderDto orderDto) {
        Integer orderId = 0;
        if (orderDto.getValidateCode() == null) {
            throw new RuntimeException("请输入验证码");
        }
        //从redis缓存中取出验证码信息
        String validateCode = (String) redisTemplate.opsForValue().get(orderDto.getTelephone() + "validateCode");
        if (!(orderDto.getValidateCode().equals(validateCode))) {
            //验证码输入错误
            throw new RuntimeException("验证码错误请重新输入");
        }
        //验证码输入正确
        Date orderDate = orderDto.getOrderDate();
        OrderSetting orderSetting = orderSettingMapper.getOrderSettingByDate(orderDate);
        orderSetting.setReservations(0);
        //检查当日是否设置了预约
        if (orderSetting == null) {
            //未设置预约
            throw new RuntimeException("当日未设置预约信息");
        }
        log.info("orderSetting的值为：{}", orderSetting.toString());
        //检查当日的预约是否满了
        if (orderSetting.getReservations() >= orderSetting.getNumber()) {
            //当日的预约已满
            throw new RuntimeException("当日的预约人数已满");
        }

        Order order = new Order();
        order.setOrderDate(orderDto.getOrderDate());
        order.setSetmealId(orderDto.getSetmealId());
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);

        //判断用户是否为会员
        Member member = memberMapper.findMemberByTelephone(orderDto.getTelephone());
        if (member != null) {
            //存在该用户
            //检查是否重复预约
            Integer memberId = member.getId();
            Order order_1 = orderMapper.findOrderByMemberId(memberId);
            if (order_1 == null || order.getOrderDate() != order_1.getOrderDate()) {
                //添加订单信息
                order.setMemberId(memberId);
                this.save(order);
                orderId = order.getId();
            } else {
                throw new RuntimeException("该用户在当天有预约");
            }
        } else {
            //不存在该用户
            //先注册为会员再添加订单信息
            Member member_1 = new Member(
                    orderDto.getName(),
                    orderDto.getSex(),
                    orderDto.getIdCard(),
                    orderDto.getTelephone(),
                    new Date());
            //保存会员信息
            memberMapper.insert(member_1);
            order.setMemberId(member_1.getId());
            this.save(order);
            orderId = order.getId();
        }
        return orderId;
    }

    @Override
    public Map<String, Object> findById4Detail(Integer id) {
        Map map = orderMapper.findById4Detail(id);
//        log.info("map集合为：{}",map.toString());
        return map;
    }

}
//            //验证码输入正确
//            Order order = new Order();
//            order.setOrderDate(orderDto.getOrderDate());
//            order.setSetmealId(orderDto.getSetmealId());
//            order.setOrderStatus(Order.ORDERTYPE_WEIXIN);
//            order.setOrderType(Order.ORDERSTATUS_NO);
